﻿using MaterialDesignThemes.Wpf;
using Newtonsoft.Json.Linq;
using RPAVIEW.util;
using RPAVIEW.views.controlLibrary;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RPAVIEW.views.EditPage.flows.IFN {
    /// <summary>
    /// Input.xaml 的交互逻辑
    /// </summary>
    public partial class Input:BaseParamViewAction{
        public Input() {
            InitializeComponent();
            IFNParam param = ParamUtil.GetCurrentParam<IFNParam>();
            List<IFCondition> cons = param.Conditions;
            if(cons != null) {
                foreach(IFCondition con in cons) {
                    StackPanel conPanel = GetConTemplate();
                    TextBox con1 = conPanel.Children[0] as TextBox;
                    TextBox con2 = conPanel.Children[2] as TextBox;
                    ComboBox relation = conPanel.Children[1] as ComboBox;
                    con1.Text = con.Con1;
                    con2.Text = con.Con2;
                    relation.SelectedIndex = (int)con.ConRelation;
                    this.conditions.Children.Add(conPanel);
                }
            }
        }

        public override void UpdateSource() {
            this.conRelation.GetBindingExpression(ComboBox.SelectedIndexProperty).UpdateSource();
            IFNParam param = ParamUtil.GetCurrentParam<IFNParam>();
            List<IFCondition> cons = new List<IFCondition>();
            foreach(StackPanel child in this.conditions.Children) {
                IFCondition con = new IFCondition();
                SelVarTextBox con1 = child.Children[0] as SelVarTextBox;
                SelVarTextBox con2 = child.Children[2] as SelVarTextBox;
                ComboBox relation = child.Children[1] as ComboBox;
                con.Con1 = con1.Text;
                con.Con2 = con2.Text;
                con.ConRelation = (ConditionRelations)Enum.Parse(typeof(ConditionRelations), relation.SelectedIndex.ToString());
                cons.Add(con);
            }
            param.Conditions = cons;
        }

        private void AddCondition(object sender, RoutedEventArgs e) {
            StackPanel con = this.GetConTemplate();
            this.conditions.Children.Add(con);
        }

        private void RemoveCondition(object sender, RoutedEventArgs e) {
            Button button = sender as Button;
            this.conditions.Children.Remove(button.Parent as StackPanel);
        }

        private void RelationSelectedChanged(object sender, SelectionChangedEventArgs e) {
            ComboBox relationObj = sender as ComboBox;
            StackPanel parent = relationObj.Parent as StackPanel;
            int selectIndex = relationObj.SelectedIndex;
            ConditionRelations relation = (ConditionRelations)Enum.Parse(typeof(ConditionRelations), selectIndex.ToString());
            if(IFCondition.singleRelation.Contains(relation)) {
                parent.Children[2].Visibility = Visibility.Collapsed;
            } else { 
                parent.Children[2].Visibility = Visibility.Visible;
            }
        }

        private StackPanel GetConTemplate() { 
            StackPanel con = new StackPanel();
            con.Orientation = Orientation.Horizontal;

            SelVarTextBox con1 = new SelVarTextBox();
            SelVarTextBox con2 = new SelVarTextBox();
            ComboBox relation = new ComboBox();
            this.AddRelationItems(relation);
            relation.SelectedIndex = 0;

            con.Children.Add(con1);
            con.Children.Add(relation);
            con.Children.Add(con2);
            
            con1.Width = 170;
            con1.Padding = new Thickness(1,1,1,1);

            con2.Width = 170;
            con2.Padding = new Thickness(1,1,1,1);

            relation.Margin = new Thickness(10, 15, 10,0);
            relation.Style = (Style)this.FindResource("LongComboBoxStyle");
            relation.Width = 100;
            relation.SelectionChanged += new SelectionChangedEventHandler(RelationSelectedChanged);

            Button button = new Button();
            button.Width = 30;
            button.Height = 20;
            button.Style = (Style)this.FindResource("MaterialDesignRaisedLightButton");
            button.ToolTip = "删除条件";
            button.HorizontalAlignment = HorizontalAlignment.Left;
            button.VerticalAlignment = VerticalAlignment.Bottom;
            button.Margin = new Thickness(5, 0, 0,5);
            button.Padding = new Thickness(0, 0, 0,0);
            button.Click += new RoutedEventHandler(RemoveCondition);
            con.Children.Add(button);

            PackIcon icon = new PackIcon();
            icon.Kind = PackIconKind.Minus;
            button.Content = icon;
            return con;
        }

        private void AddRelationItems(ComboBox relations) {
            foreach(string relaName in IFCondition.relationAndName.Values) { 
                ComboBoxItem item = new ComboBoxItem();
                item.Content = relaName;
                relations.Items.Add(item);
            }
        }
    }
}
